package com.ruoyi;

import com.google.common.annotations.VisibleForTesting;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.uuid.IdUtils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class Demo01 {
    private String secret="abcdefghijklmnopqrstuvwxyz";

    /**
     * 根据UUID生成一个Token
     */
    @Test
    public void test2(){
        /**
         * UUID:66332159-09a8-47f5-8934-adc9932e594d
         * tokenNew:eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjY2MzMyMTU5LTA5YTgtNDdmNS04OTM0LWFkYzk5MzJlNTk0ZCJ9.Rb1EXc1cqyMDoknsZI7ijLvyiClQ-4KhaFz7MyfPyAZPPxV4JekZ18Ds85snY0YSQQ4L5YgawUKYe77TA5p9zw
         */
        String uuid = IdUtils.fastUUID();
        System.err.println("UUID:"+uuid);
        Map<String, Object> claims = new HashMap<>();
        claims.put(Constants.LOGIN_USER_KEY, uuid);
        String tokenNew = Jwts.builder()
                .setClaims(claims)
                .signWith(SignatureAlgorithm.HS512, secret).compact();
        System.err.println("tokenNew:"+tokenNew);
    }

    /**
     * 再根据Token，从里面获取到UUID
     */
    @Test
    public void test1() {
        /**
         * 可以根據Token獲取到之前生成的UUID
         * uuid:66332159-09a8-47f5-8934-adc9932e594d
         */
        Claims claims = Jwts.parser()
                .setSigningKey(secret)
                .parseClaimsJws("eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjY2MzMyMTU5LTA5YTgtNDdmNS04OTM0LWFkYzk5MzJlNTk0ZCJ9.Rb1EXc1cqyMDoknsZI7ijLvyiClQ-4KhaFz7MyfPyAZPPxV4JekZ18Ds85snY0YSQQ4L5YgawUKYe77TA5p9zw")
                .getBody();
       String uuid = (String)claims.get(Constants.LOGIN_USER_KEY);
        System.err.println("uuid:"+uuid);
    }


}
